package com.servlet;

import com.bean.Message;
import com.service.impl.MessageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/MessageServlet/*")
public class MessageServlet extends HttpServlet {

    MessageServiceImpl messageService = new MessageServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html,charset=utf-8");//命令浏览器用什么编码打开文件

        String pathInfo = request.getPathInfo().substring(1);
        if("listAll".equals(pathInfo)){
            listAll(request,response);
        }else if("listByName".equals(pathInfo)){
            listByName(request,response);
        }else if("deleteById".equals(pathInfo)){
            deleteById(request,response);
        }else if("messageAdd".equals(pathInfo)){
           messageAdd(request,response);
        }
    }

    private void messageAdd(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("userName");
        String content = request.getParameter("content");
        String time = request.getParameter("time");
        Message message = new Message(null,userName,content,time);
        try {
            boolean b = messageService.insert(message);
            if(b){
                request.setAttribute("msg","留言成功");
            }else{
                request.setAttribute("msg","留言失败");
            }
            request.getRequestDispatcher("/property/message_add.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        try {
            boolean b = messageService.deleteById(id);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private void listByName(HttpServletRequest request, HttpServletResponse response) {
            String name = (String)request.getSession().getAttribute("name");
        System.out.println(name);
        try {
            List<Message> mList = messageService.selectByName(name);

            request.setAttribute("mList",mList);
            request.getRequestDispatcher("/property/message_only.jsp").forward(request,response);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAll(HttpServletRequest request, HttpServletResponse response) {

        try {
            List<Message> messageList = messageService.selectAll();

            request.setAttribute("messageList",messageList);
            request.getRequestDispatcher("/property/message.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}